package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("userAddressBookController")
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    @ApiOperation("新增地址")
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据用户ID查询所有地址")
    public Result<List<AddressBook>> list(){
        List<AddressBook> result=addressBookService.list();
        return Result.success(result);
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> defaultAddressBook(){
        AddressBook result=addressBookService.getDefaultAddressBook();
        return Result.success(result);
    }

    @PutMapping
    @ApiOperation("根据ID修改地址")
    public Result updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据ID删除地址")
    public Result deleteAddressBook(@RequestParam Long id){
        addressBookService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询地址")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id){
        AddressBook result=addressBookService.getADDressBookById(id);
        return Result.success(result);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result updateDefaultAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateDefaultAddressBook(addressBook);
        return Result.success();
    }
}
